﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommandService.Base
{
    public class BaseCommon
    {
        /// <summary>
        /// 将datatable转为list集合
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<Dictionary<string, string>> TableToDictionary(DataTable dt)
        {
            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            Dictionary<string, string> dic;
            try
            {
                if (dt.Rows.Count > 0)
                {
                    int colCount = dt.Columns.Count;
                    foreach (DataRow row in dt.Rows)
                    {
                        dic = new Dictionary<string, string>();
                        for (int i = 0; i < colCount; i++)
                        {
                            dic.Add(dt.Columns[i].ToString(), row[i].ToString());
                        }
                        list.Add(dic);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return list;
        }

        public List<T> TableTolist<T>(DataTable table)
        {
            // 定义集合  
            List<T> list = new List<T>();

            // 获得此模型的类型  
            Type type = typeof(T);
            //定义一个临时变量  
            string tempName = string.Empty;
            //遍历DataTable中所有的数据行  
            foreach (DataRow dr in table.Rows)
            {
                T t = (T)Activator.CreateInstance(typeof(T));
                // 获得此模型的公共属性  
                PropertyInfo[] propertys = t.GetType().GetProperties();
                //遍历该对象的所有属性  
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;//将属性名称赋值给临时变量  

                    if (table.Columns.Contains(tempName))//检查DataTable是否包含此列（列名==对象的属性名）    
                    {
                        // 判断此属性是否有Setter  
                        if (!pi.CanWrite) continue;//该属性不可写，直接跳出  
                        //取值  
                        object value = dr[tempName];
                        #region  //如果非空，则赋给对象的属性
                        if (value != DBNull.Value)
                        {
                            Type ty;
                            if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                            {
                                ty = Nullable.GetUnderlyingType(pi.PropertyType);
                            }
                            else
                            {
                                ty = pi.PropertyType;
                            }
                            if (ty.Name.Trim().ToUpper().IndexOf("INT") > -1)
                            {
                                pi.SetValue(t, Convert.ToInt32(value), null);
                            }
                            else
                            {
                                pi.SetValue(t, value.ToString(), null);
                            }
                        }
                        #endregion
                    }
                }
                //对象添加到泛型集合中  
                list.Add(t);
            }
            return list;

        }
    }
}
